<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="{{ asset('admin/Widget/layui/lib/layui/css/layui.css') }}">
    <script src="{{ asset('admin/js/jquery-1.9.1.min.js') }}"></script>
    <script src="{{ asset('admin/Widget/layui/lib/layui/layui.js') }}"></script>
    <script type="text/javascript" src="{{ asset('admin/Widget/Validform/5.3.2/Validform.min.js') }}"></script>
    <title>Document</title>
    <style>
        .idcard img{
            max-width: 40%;
        }
    </style>
</head>

<body>
<div class="layui-container">
        {!! Form::open(['route' => 'rider.store', 'method' => 'post','id'=>'form','class' => 'layui-form']) !!}
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name"  autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="tel"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱（登录账户）</label>
            <div class="layui-input-block">
                <input type="text" name="email"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item idcard">
            <label class="layui-form-label">身份证正面</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="upload1">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="idcard1">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item idcard">
            <label class="layui-form-label">身份证反面</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="upload2">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="idcard2">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">银行卡</label>
            <div class="layui-input-inline" style="min-width: 50%;">
                <input type="text" name="card_no"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="bank_id" lay-verify="required">
                    <option value="">选择银行类型</option>
                    @foreach($banks as $bank)
                    <option value="{{ $bank->id }}">{{ $bank->name }}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="card_positive" id="card_positive">
                <input type="hidden" name="card_reverse" id="card_reverse">
                <button class="layui-btn" type="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    {!! Form::close() !!}
</div>

<script>
    layui.use(['form', 'upload'], function () {
        var form = layui.form,
            $ = layui.jquery,
            upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#upload1',
            url: '{{ route('rider.uploadCard') }}' //改成您自己的上传接口
            ,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#idcard1').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.status === 200) {
                        $('#card_positive').val(res.data.src);
                    return layer.msg('上传身份证正面成功');
                }
                //上传成功
            },
            error: function () {}
        });
        var uploadInst = upload.render({
            elem: '#upload2',
            url: '{{ route('rider.uploadCard') }}' //改成您自己的上传接口
            ,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#idcard2').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.status === 200) {
                    $('#card_reverse').val(res.data.src);
                    return layer.msg('上传身份证反面成功');
                }
                //上传成功
            },
            error: function () {}
        });
    });
</script>

<script>
    $('#form').Validform({
        tiptype : 4,
        ajaxPost : true,
        beforeCheck:function(curform){
            console.log(curform);
            //在表单提交执行验证之前执行的函数，curform参数是当前表单对象。
            //这里明确return false的话将不会继续执行验证操作;
            index = layer.load(1, {shade: [0.6, '#393D49']});
        },
        callback : function (res) {
            console.log(res);
            if(res.status == 422) {
                var json=JSON.parse(res.responseText);
                if(json.hasOwnProperty("errors")){
                    $.each(json.errors, function(idx, obj) {
                        layer.msg(obj[0], {icon: 5});
                        layer.close(index);
                        return false;
                    });
                } else {
                    layer.msg(json.msg, {icon: 5});
                    layer.close(index);
                    return false;
                }
            } else if(res.status== 200) {
                layer.msg(res.msg, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                   parent.location.href = '{{ route('rider.index') }}'
                });

            }
        },
    });
</script>
</body>

</html>